package com.wangpu.oa.web.captcha;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class CaptchaUtil {
	private static final String CHARS="ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789"; 
	
	public static String randCode(int length) {
		char[] result=new char[length];
		for(int i=result.length;i-->0;) {
			result[i]=CHARS.charAt((int) (Math.random()*CHARS.length()));
		}
		return new String(result);
	}
	
	public static BufferedImage generate(String code,int width,int height) {
		BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		
		Graphics g=img.getGraphics();
		g.setColor(Color.green);
		g.fillRect(0, 0, width, height);
		
		g.setColor(Color.blue);
		g.setFont(new Font("黑体", Font.BOLD, 30));
		g.drawString(code, 30, 30);
		
		g.setColor(Color.darkGray);
		g.drawArc(10, 10, width, height, 80, 60);
		return img;
	}
	
	
}
